#!/usr/bin/perl
# CCNCore::spool::create_sqlite.pm
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

package CCNCore::spool::create_sqlite;
use strict; use warnings;

# Run from a GUI node.


=pod

    Note: This is only a proof-of-concept for release 2.1.
    In future release we need to become much more clever about how we select CDNs and systems
    that need to be updated (using dirty bits, etc.).

=cut

our $D;

sub run
{
    my $ticket   = pop;
    my $callback = pop;
    
    my $target_pk = shift; # Optional: server.pk to generate for and deliver to

    # Use the current configuration schema to distribute the new one
    use CCNCore::Provisioning::Local;
    my $p = new CCNCore::Provisioning::Local;

    use CCNCore::create_sqlite;
    my $version = CCNCore::create_sqlite::run($p,$target_pk);
    $p->die ("Create SQLite failed") if not defined $version;

    # Re-open the new database to get the new list of servers.
    # (This is important when a new server just got added.)
    undef $p;
    $p = new CCNCore::Provisioning::Local;
    
    # This is ran on a GUI node, which means we propagate only to the CDN downstream servers
    use CCNCore::Provision::CDN::distribute_configuration;
    my $b = new CCNCore::Provision::CDN::distribute_configuration($p);

    use CCNCore::IPC::Ticket;
    my $t = new CCNCore::IPC::Ticket;
    $t->add_context('callback',$callback);
    $t->add_context('ticket',$ticket);
    my $count = $b->distribute_high($t,$target_pk);
    # Wait for 'cdn-downstream-high-completed configuration'
}

1;